package com.fram.controller.api;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.api.news.request.CommentSearchRequest;
import com.fram.bean.api.news.request.NewsSearchRequest;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.entity.NewsComment;
import com.fram.entity.ResOperate;
import com.fram.service.api.v1.NewsService;

/**
 * @作者： 陳曉鬆 @时间： 2020年4月28日 - 下午6:23:25 @description：-> 新闻模块
 */
@RestController
@RequestMapping("/api/{version}/news")
public class NewsController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(NewsController.class);

	@Resource(name = "newsService")
	private NewsService newsService;

	/** 新闻列表 */
	@ApiVersion(1.0)
	@RequestMapping("list")
	public ResponseMsg list(@RequestBody PageBean<NewsSearchRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(newsService.selectForApiList(request));
		} catch (ProException e) {
			LOG.error("-新闻-列表获取出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-新闻-列表获取异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 新闻详情 */
	@ApiVersion(1.0)
	@RequestMapping("detail")
	public ResponseMsg detail(Long newsId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			Long userId = null;
			try {
				userId = getApiUserId();
			} catch (Exception e) {
			}
			msg.setData(newsService.newsDetail(newsId, userId));
		} catch (ProException e) {
			LOG.error("-新闻-新闻详情出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-新闻-新闻详情异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 新闻评论列表 */
	@ApiVersion(1.0)
	@RequestMapping("comment/list")
	public ResponseMsg commentList(@RequestBody PageBean<CommentSearchRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			try {
				request.getParam().setUserId(getApiUserId());
			} catch (Exception e) {
			}
			msg.setData(newsService.selectCommentForApiList(request));
		} catch (ProException e) {
			LOG.error("-新闻-评论列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-新闻-评论列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	/** 资源操作 */
	@ApiVersion(1.0)
	@RequestMapping("auth/newsOperate")
	public ResponseMsg newsOperate(@RequestBody ResOperate operate) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			operate.setUserId(getApiUserId());
			newsService.newsOperate(operate);
		} catch (ProAffairException e) {
			LOG.error("-新闻-资源操作出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-新闻-资源操作异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 发布评论 */
	@ApiVersion(1.0)
	@RequestMapping("auth/comment/add")
	public ResponseMsg newcomment(@RequestBody NewsComment comment) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			comment.setUserId(getApiUserId());
			msg.setData(newsService.addComment(comment));
		} catch (ProAffairException e) {
			LOG.error("-新闻-发布评论->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-新闻-发布评论->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

}
